package com.O1DesignModel;

/**
 * 装饰器模式，对原来的方法进行二次装饰，或者进一步改造，避免使用继承，达到扩展的目的
 *
 */

// 饮料接口
interface Beverage{
    public String getDescription();
    public double cost();
}

/**
 * 基础的coffee接口
 */
class CoffeeBeverage implements Beverage{
    public String getDescription()
    {
        return "Coffee";
    }
    public double cost()
    {
        return 10.0;
    }
}

abstract class BeverageDecorator implements Beverage{
    protected Beverage beverage;
    public BeverageDecorator(Beverage beverage)
    {
        this.beverage = beverage;
    }

    public String getDescription()
    {
        return beverage.getDescription();
    }

    public double cost()
    {
        return beverage.cost();
    }
}

class MilkDecorator extends BeverageDecorator{
    BeverageDecorator decorator;
    public MilkDecorator(Beverage beverage){
        super(beverage);
    }

    public String getDescription()
    {
        return beverage.getDescription() + "Milk";
    }

    public double cost()
    {
        return beverage.cost() + 2.0;
    }
}

class SogarDecorator extends BeverageDecorator{
    public SogarDecorator(Beverage beverage){
        super(beverage);
    }

    public String getDescription()
    {
        return beverage.getDescription() + "Sogar";
    }

    @Override
    public double cost() {
        return beverage.cost()+3.0;
    }
}
public class O4_DecoratorPattern {
    public static void main(String[] args) {
        Beverage coffee = new CoffeeBeverage();
        System.out.println(coffee.getDescription() + ": " + coffee.cost());
        Beverage milkCoffee = new MilkDecorator(coffee);
        System.out.println(milkCoffee.getDescription() + ": " + milkCoffee.cost());
        Beverage sugarCoffee = new SogarDecorator(coffee);
        System.out.println(sugarCoffee.getDescription() + ": " + sugarCoffee.cost());
    }
}
